package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirAirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.Date;

@Service
public class AircraftServiceImpl implements IAirCraftService<AirAirCraft> {
    @Autowired
    private AirCraftMapper airCraftMapper;

    @Override
    public AirAirCraft queryById(String id) {
        AirAirCraft airAirCraft = airCraftMapper.selectById(id);
        if (airAirCraft == null){
            throw new RuntimeException("查询失败！当前id：" + id);
        }
        return airAirCraft;
    }

    @Override
    public PageVo<AirAirCraft> queryByPage(Integer pageNumber, Integer pageSize , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirAirCraft> queryWrapper  = new QueryWrapper<>();
        //支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        //支持指定字段排序
        if (StrUtil.isNotBlank(oderByField) && orderByAsc){
            queryWrapper.orderByAsc(oderByField);
        }
        IPage<AirAirCraft> pageParam = new Page<>(pageNumber,pageSize);
        IPage<AirAirCraft> airCraftIPage = airCraftMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(airCraftIPage);
    }

    @Override
    public AirAirCraft insert(AirAirCraft airAircraft) {
        if (airCraftMapper.exists(new QueryWrapper<AirAirCraft>().eq("icao_code", airAircraft.getIcaoCode()))) {
            throw new RuntimeException("ICAO地址码已存在");
        }
        if (airCraftMapper.exists(new QueryWrapper<AirAirCraft>().eq("registration", airAircraft.getRegistration()))) {
            throw new RuntimeException("注册号已存在");
        }
        airAircraft.setCreateTime(new Date());
        airAircraft.setUpdateTime(new Date());
        int insert = airCraftMapper.insert(airAircraft);
        if (insert <= 0){
            throw new RuntimeException("添加失败 ->>> " + airAircraft);
        }
        return airAircraft;
    }

    @Override
    public AirAirCraft update(AirAirCraft airAircraft) {
        if (airCraftMapper.exists(new QueryWrapper<AirAirCraft>().eq("icao_code", airAircraft.getIcaoCode()).ne("id",airAircraft.getId()))) {
            throw new RuntimeException("ICAO地址码已存在");
        }
        if (airCraftMapper.exists(new QueryWrapper<AirAirCraft>().eq("registration", airAircraft.getRegistration()).ne("id",airAircraft.getId()))) {
            throw new RuntimeException("注册号已存在");
        }
        airAircraft.setUpdateTime(new Date());
        int i = airCraftMapper.updateById(airAircraft);
        if (i <= 0){
            throw new RuntimeException("修改失败 ->>> id：" + airAircraft.getId() + " 不存在； 实体信息 ->>> " + airAircraft);
        }
        return airAircraft;
    }

    @Override
    public void deleteById(String id) {
        int i = airCraftMapper.deleteById(id);
        if (i <= 0){
            throw new RuntimeException("删除失败 ->>> id：" + id);
        }
    }
}
